package com.xxx.rpc.common.xml.xsd;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.w3c.dom.Element;

import com.xxx.rpc.common.xml.Dubbo;
import com.xxx.rpc.common.xml.tems.Application;
import com.xxx.rpc.common.xml.tems.Protocol;
import com.xxx.rpc.common.xml.tems.Reference;
import com.xxx.rpc.common.xml.tems.Registry;
import com.xxx.rpc.common.xml.tems.Service;
import com.xxx.rpc.common.xml.valueresolver.EmbeddedValueResolverAwareBeanPostProcessor;


public class DubboBeanDefinitionParser implements BeanDefinitionParser {
	private final Class<?> beanClass;
	
	private final RootBeanDefinition beanDefinition;

	public DubboBeanDefinitionParser(Class<?> beanClass,RootBeanDefinition beanDefinition) {
		this.beanClass = beanClass;
		this.beanDefinition=beanDefinition;
	}

	public BeanDefinition parse(Element element, ParserContext parserContext) {
		if (Application.class.equals(beanClass)) {
			Application application = new Application();
			application.setName(element.getAttribute("name"));
			beanDefinition.getPropertyValues().add("application", application);
		} else if (Registry.class.equals(beanClass)) {
			Registry registry = new Registry();
			registry.setAddress(element.getAttribute("address"));
			beanDefinition.getPropertyValues().add("registry", registry);
		} else if (Protocol.class.equals(beanClass)) {
			Protocol protocol = new Protocol();
			protocol.setName(element.getAttribute("name"));
			protocol.setPortStr(element.getAttribute("port"));
			beanDefinition.getPropertyValues().add("protocol", protocol);
		} else if (Service.class.equals(beanClass)) {
			@SuppressWarnings("unchecked")
			List<Service> serviceList = (List<Service>) beanDefinition.getPropertyValues().get("serviceList");
			if(serviceList==null) {
				serviceList=new ArrayList<>();
			}
			Service service=new Service();
			service.setInterfaceClazz(element.getAttribute("interface"));
			String refBeanName = element.getAttribute("ref");
			service.setRef(refBeanName);
			serviceList.add(service);
			beanDefinition.getPropertyValues().add("serviceList", serviceList);
		} else if (Reference.class.equals(beanClass)) {
			@SuppressWarnings("unchecked")
			List<Reference> referenceList = (List<Reference>) beanDefinition.getPropertyValues().get("referenceList");
			if(referenceList==null) {
				referenceList=new ArrayList<>();
			}
			Reference reference=new Reference();
			reference.setInterfaceClazz(element.getAttribute("interface"));
			String refBeanName = element.getAttribute("id");
			reference.setId(refBeanName);
			referenceList.add(reference);
			beanDefinition.getPropertyValues().add("referenceList", referenceList);
		}
		
		BeanDefinitionRegistry beanDefinitionRegistry = parserContext.getRegistry();
		beanDefinitionRegistry.registerBeanDefinition(Dubbo.class.getName(), beanDefinition);// 注册bean到BeanDefinitionRegistry中
		Boolean has = beanDefinitionRegistry.containsBeanDefinition(EmbeddedValueResolverAwareBeanPostProcessor.class.getName());
		if(!has) {
			RootBeanDefinition embeddedValueResolverAwareBeanPostProcessorBeanDefinition=new RootBeanDefinition(EmbeddedValueResolverAwareBeanPostProcessor.class);
			beanDefinitionRegistry.registerBeanDefinition(EmbeddedValueResolverAwareBeanPostProcessor.class.getName(), embeddedValueResolverAwareBeanPostProcessorBeanDefinition);
		}
		return beanDefinition;
	}
}